//
//  UCSetable.swift
//  UICreate
//
//  Created by JY_NEW on 2021/10/27.
//

import Foundation

///所有需要设置样式的都可继承该协议
public protocol UCSetable: AnyObject { }

///UIView默认可设置样式
extension UIView: UCSetable { }
///UICollectionViewLayout默认可设置样式
extension UICollectionViewLayout: UCSetable { }

extension UCSetable {
    @discardableResult
    public func set(animated duration: TimeInterval, settings: [UCSetting<Self>]) -> Self {
        if duration > 0 {
            UIView.animate(withDuration: duration) {
                settings.forEach({$0.action(self)})
            }
        }else{
            settings.forEach({$0.action(self)})
        }
        return self
    }
    @discardableResult
    public func set(animated: Bool = false,_ settings:UCSetting<Self>...) -> Self {
        return self.set(animated: animated ? 0.25 : 0, settings: settings)
    }
    @discardableResult
    public func set(animated duration: TimeInterval,_ settings:UCSetting<Self>...) -> Self {
        return self.set(animated: duration, settings: settings)
    }
}

extension UCSetable where Self: UIView {
    public static func create(animated: Bool = false,_ settings:UCSetting<Self>...) -> Self {
        return Self().set(animated: animated ? 0.25 : 0, settings: settings)
    }
    public static func create(animated duration: TimeInterval,_ settings:UCSetting<Self>...) -> Self {
        return Self().set(animated: duration, settings: settings)
    }
    public init(animated: Bool = false,_ settings:UCSetting<Self>...) {
        self.init()
        self.set(animated: animated ? 0.25 : 0, settings: settings)
    }
    public init(animated duration: TimeInterval,_ settings:UCSetting<Self>...) {
        self.init()
        self.set(animated: duration, settings: settings)
    }
}
extension Array {
    @discardableResult
    public func set(animated duration: TimeInterval, settings:[UCSetting<Element>]) -> Self {
        if duration > 0 {
            UIView.animate(withDuration: duration) {
                self.forEach { el in
                    settings.forEach({ $0.action(el) })
                }
            }
        }else{
            self.forEach { el in
                settings.forEach({ $0.action(el) })
            }
        }
        return self
    }
    @discardableResult
    public func set(animated: Bool = false,_ settings:UCSetting<Element>...) -> Self {
        return self.set(animated: animated ? 0.25 : 0, settings: settings)
    }
    @discardableResult
    public func set(animated duration: TimeInterval,_ settings:UCSetting<Element>...) -> Self {
        return self.set(animated: duration, settings: settings)
    }
}

///全局样式表
var __UCGlobalSettings: [String:Any] = [:]
extension UCSetable {
    ///设置全局样式
    public static func set(_ settings:UCSetting<Self>...) {
        let className = String(describing: type(of: Self.self))
        __UCGlobalSettings[className] = settings
    }
    ///清除全局样式
    public static func clearGlobalSettings() {
        let className = String(describing: type(of: Self.self))
        __UCGlobalSettings[className] = nil
    }
    ///加载全局样式
    public func loadGlobalSettings() {
        let className = String(describing: type(of: Self.self))
        if let settings = __UCGlobalSettings[className] as? [UCSetting<Self>] {
            settings.forEach({ $0.action(self) })
        }
    }
}
